package com.rq.base.ui.img_load;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.rq.base.util.LOG;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;


/**
 * @author RQ
 * date    2022-01-27 15:58
 */
public class DefaultProxy implements ImageLoadProxyImpl {
    @Override
    public void display(Context context, Object obj, final ImageView view, int defaultRes, final int errRes,
                        int width, int height, int moreTag) {
        if (obj == null) {
            LOG.e("DefaultProxy", "display.null");
            if (errRes != 0) {
                view.post(new Runnable() {
                    @Override
                    public void run() {
                        view.setImageResource(errRes);
                    }
                });
            }
            return;
        }
        String urlPath = null;
        URL fileUrl = null;
        if (obj instanceof String) {
            LOG.e("DefaultProxy", "display.34:" + obj);
            urlPath = (String) obj;
        } else if (obj instanceof URL) {
            fileUrl = (URL) obj;
        } else {
            LOG.e("DefaultProxy", "默认模式使用错误,当前加载内容:" + obj.getClass().getSimpleName());
            if (errRes != 0) {
                view.post(new Runnable() {
                    @Override
                    public void run() {
                        view.setImageResource(errRes);
                    }
                });
            }
            return;
        }
        final String url = urlPath;
        final URL finalFileUrl = fileUrl;
        new Thread() {
            @Override
            public void run() {
                URL myFileUrl = null;
                Bitmap bitmap = null;
                if (finalFileUrl != null) {
                    myFileUrl = finalFileUrl;
                } else {
                    try {
                        myFileUrl = new URL(url);
                    } catch (MalformedURLException e) {
                        if (errRes != 0) {
                            view.post(new Runnable() {
                                @Override
                                public void run() {
                                    view.setImageResource(errRes);
                                }
                            });
                        } else {
                            e.printStackTrace();
                        }
                        return;
                    }
                }
                try {
                    HttpURLConnection conn;
                    if (myFileUrl.getPath().startsWith("https")) {
                        conn = (HttpsURLConnection) myFileUrl.openConnection();
                    } else {
                        conn = (HttpURLConnection) myFileUrl.openConnection();
                    }
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    if (errRes != 0) {
                        view.post(new Runnable() {
                            @Override
                            public void run() {
                                view.setImageResource(errRes);
                            }
                        });
                    } else {
                        e.printStackTrace();
                    }
                    return;
                }
                final Bitmap finalBitmap = bitmap;
                view.post(new Runnable() {
                    @Override
                    public void run() {
                        view.setImageBitmap(finalBitmap);
                    }
                });
            }
        }.start();
    }
}
